﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class student_submitAnswer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string radioStr = Request["radioStr"].ToString().Trim();
        string mutilStr = Request["mutilStr"].ToString().Trim();
        string judgeStr = Request["judgeStr"].ToString().Trim();
        string answerStr = Request["answerStr"].ToString().Trim();
        string test_question_id = Request["test_question_id"].ToString().Trim(); 
        string student_id = Session["student_id"].ToString();

        string sql = "INSERT INTO [dbo].[tb_test_questions_answer]" +
                                "           ([student_id]" +
                                "           ,[question_id]" +
                                "           ,[question_type_id]" +
                                "           ,[student_answer]" +
                                "           ,[test_question_id]" +
                                "           ,[question_score])" +
                                "     VALUES";
        if (radioStr != "")
        {
            DataTable dt = (DataTable)Session["radio_answer"];
            string[] radio = radioStr.Split('`');//题目分隔符
            string str = "";
            for (int i = 0; i < radio.Length-1; i++)
            {
                string[] item =  radio[i].Split('#');
                //判断答案是否正确
                string  question_score = "0";
                if (dt.Rows[i][0].ToString() == item[1])
                {
                    question_score = dt.Rows[i][1].ToString();
                }
                str = String.Format("('{0}',{1},'1','{2}','{3}',{4}),", student_id, item[0], item[1], test_question_id, question_score);
                sql = sql + str;
            }
        }
        if (mutilStr != "")
        {

            DataTable dt = (DataTable)Session["mutil_answer"];
            string[] mutil = mutilStr.Split('`');
            string str = "";
            for (int i = 0; i < mutil.Length - 1; i++)
            {
                string[] item = mutil[i].Split('#');
                //判断答案是否正确
                string question_score = "0";
                if (dt.Rows[i][0].ToString() == item[1]) //全对才有分
                {
                    question_score = dt.Rows[i][1].ToString();
                }
                str = String.Format("('{0}',{1},'2','{2}','{3}',{4}),", student_id, item[0], item[1], test_question_id,question_score);
                sql = sql + str;
            }
            
        }
        if (judgeStr != "")
        {

            DataTable dt = (DataTable)Session["judge_answer"];
            string[] judge = judgeStr.Split('`');
            string str = "";
            for (int i = 0; i < judge.Length - 1; i++)
            {
                string[] item = judge[i].Split('#');
                if (item[1] == "true")
                {
                    item[1] = "1";
                }
                else
                {
                    item[1] = "0";
                }
                //判断答案是否正确
                string question_score = "0";
                if (dt.Rows[i][0].ToString() == item[1]) 
                {
                    question_score = dt.Rows[i][1].ToString();
                }
                str = String.Format("('{0}',{1},'3','{2}','{3}',{4}),", student_id, item[0], item[1], test_question_id,question_score);
                sql = sql + str;
            }

        }
        if (answerStr != "")
        {

            DataTable dt = (DataTable)Session["answer_answer"];
            string[] answer = answerStr.Split('`');
            string str = "";

            for (int i = 0; i < answer.Length - 1; i++)
            {
                string[] item = answer[i].Split('#');
                //判断答案是否正确
                string question_score = "0";
                if (dt.Rows[i][0].ToString() == item[1])
                {
                    question_score = dt.Rows[i][1].ToString();
                }

                str = String.Format("('{0}',{1},'4','{2}','{3}',{4}),", student_id, item[0], item[1], test_question_id, question_score);
                sql = sql + str;
            }
        }
        sql = sql.Substring(0, sql.Length-1);
        int k = SqlServer.ExecuteNonQuery(sql);
        if (k < 1)
        {
            Response.Write(k.ToString());
            Response.End();
            return;
        }
        //计算成绩
        //获取试题答案和分数
        string score = "0";
        string sql1 = "select sum(question_score) from tb_test_questions_answer where student_id ='" + student_id + "' and test_question_id ='" + test_question_id + "'";
        score = SqlServer.ExecuteScalar(sql1);
        if (score == "NULL")
        {
            score = "0";
        }

        string sqlScore = "update tb_score set test_score =" + score + " where student_id ='" + student_id 
            + "' and course_id = (select course_id from tb_test_info where test_question_id ='" + test_question_id + "')";
        int t = SqlServer.ExecuteNonQuery(sqlScore);
        if (t >= 0)
        {
            Response.Write(score);//返回分数
        }
        else
        {
            Response.Write("-1");//保存失败
        }
        Response.End();
    }
}